<?php
/**
 * Created by PhpStorm.
 * User: lee
 * Date: 2018-12-26
 * Time: 11:11
 */

namespace Biz\module\Classes\traits;
use Biz\tools\Amount;

trait AmountStruct{
    public static function renderAmountStruct($renderData=[]){
        if(empty($renderData)){
            return (array)[];
        }

        $result = [];

        //判断免邮
        if($renderData['shipping_fee']>0 || $renderData['shipping_amount']>0){
            if($renderData['shipping_discount']>0){
                $result[] = [
                    'name'=>'运费',
                    'icon'=>'',
                    'text'=>"¥ ".Amount::format($renderData['shipping_fee'])
                ];
                $result[] = [
                    'name'=>'运费优惠',
                    'icon'=>'',
                    'text'=>"-¥ ".Amount::format($renderData['shipping_discount'])
                ];
            }elseif($renderData['shipping_discount'] == 0){

                $result[] = [
                    'name' => '运费',
                    'icon' => '',
                    'text' => "¥ " . Amount::format($renderData['shipping_amount'])
                ];
            }

        }else{
            if($renderData['shipping_fee'] == 0){
                $result[] = [
                    'name'=>'运费',
                    'icon'=>'',
                    'text'=>"包邮"
                ];
            }
        }

        foreach ($renderData as $key=>$value){
            if($value == 0){
                continue;
            }

            $value = Amount::format($value);

            switch ($key){
                case 'integral_amount':
                    $result[] = [
                        'name'=>'积分优惠',
                        'icon'=>'',
                        'text'=>"-¥ $value"
                    ];
                    break;
                case 'coupon_amount':
                    $result[] = [
                        'name'=>'平台券优惠',
                        'icon'=>'',
                        'text'=>"-¥ $value"
                    ];
                    break;
                case 's_coupon_amount':
                    $result[] = [
                        'name'=>'店铺券优惠',
                        'icon'=>'',
                        'text'=>"-¥ $value"
                    ];
                    break;
                case 'meal_amount':
                    $result[] = [
                        'name'=>'套餐优惠',
                        'icon'=>'',
                        'text'=>"-¥ $value"
                    ];
                    break;
                case 'red_packet':
                    $result[] = [
                        'name'=>'现金红包',
                        'icon'=>'',
                        'text'=>"-¥ $value"
                    ];
                    break;
                case 'pay_amount':
                    $result[] = [
                        'name'=>'实付金额',
                        'icon'=>'',
                        'text'=>"¥ $value"
                    ];
                    break;
            }
        }
        return $result;
    }
}